from django.db import models
import const

class Module(models.Model):
    LAYER_CHOICES = (
        (const.LAYERS['Applications'], 'Applications'),
        (const.LAYERS['Development'], 'Development'),
        (const.LAYERS['Desktop (GNOME)'], 'Desktop (GNOME)'),
        (const.LAYERS['System'], 'System'),
        (const.LAYERS['Web Services'], 'Web Services'),
        (const.LAYERS['Meta Packages'], 'Meta Packages'),
    )
    name = models.CharField(max_length=64)
    layer = models.IntegerField(
        max_length=1, 
        choices=LAYER_CHOICES)
    class Meta:
        unique_together = ('name', 'layer')

class OSRelease(models.Model):
    name = models.CharField(max_length=32, unique=True)

class PackageName(models.Model):
    name = models.CharField(max_length=128, unique=True)
    is_normal = models.BooleanField()

#store initial packages of each os_release(like s11, s12) and record all changes of packages in builds
class Package(models.Model):
    CHANGE_FLAG_CHOICES = (
        (const.CHANGE_FLAGS['a'], 'a'),
        (const.CHANGE_FLAGS['o'], 'o'),
        (const.CHANGE_FLAGS['m'], 'm'),
        (const.CHANGE_FLAGS['r'], 'r'),
        (const.CHANGE_FLAGS['i'], 'i'),
        (const.CHANGE_FLAGS['v'], 'v'),
    )
    package_name = models.ForeignKey(PackageName)
    module = models.ForeignKey(Module)
    os_release = models.ForeignKey(OSRelease)
    change_in_build = models.CharField(max_length=3)
    #versin of this package in latest build, so it is always the newest version
    version = models.CharField(max_length=64)
    change_flag = models.IntegerField(
        max_length=1, 
        choices=CHANGE_FLAG_CHOICES)

    class Meta:
        unique_together = ('package_name', 'os_release', 'change_in_build')


class FixedBug(models.Model):
    os_release = models.ForeignKey(OSRelease)
    build_no = models.CharField(max_length=3)
    bug_id = models.CharField(max_length=64)
    bug_summary = models.CharField(max_length=512)

#record all changes of package in builds
class ChangedPackage(models.Model):
    build_no = models.CharField(max_length=3)
    package = models.ForeignKey(Package) #contains package and os_release info
    current_version = models.CharField(max_length=64) #the current version of package in this build
    old_version = models.CharField(max_length=64) #version of this package before change


class Dependancy(models.Model):
    origin_package = models.ForeignKey(PackageName, related_name='origin_package')
    depend_package = models.ForeignKey(PackageName, related_name='depend_package')

    class Meta:
        unique_together = ('origin_package', 'depend_package')

class DependancyOfBuild(models.Model):
    relation = models.ForeignKey(Dependancy)
    os_release = models.ForeignKey(OSRelease)
    build_no = models.CharField(max_length=3)

    class Meta:
        unique_together = ('relation', 'os_release', 'build_no')
